<?php
class model_interview extends model_weixin{
	
	/**
	 * @info 新增走訪信息
	 * @author jackie yang
	 * @datetime 20140705
	 * @param  $data  array
	 * @param $table string 数据表
	 */
	public static function add_interview_record($data,$table) {
		$sql = "INSERT INTO " . $table . " SET ";
		$binds = array ();
		foreach ( $data as $k => $v ) {
			$sql .= '`' . $k . '`=:' . $k . ',';
			$binds [':' . $k] = $v;
		}
		$sql .= 'raw_add_time=NOW(),raw_update_time=now()';
		try {
			$arr = self::DB ()->dbh->prepare ( $sql );
			$rel = $arr->execute ( $binds );
			if ($rel == TRUE) {
				return self::DB ()->dbh->lastInsertId ();
			}
		} catch ( Exception $e ) {
			framework_static_function::write_log ( __METHOD__ . ":" . $e->getMessage (), 'weixin_sql' );
		}
		return 0;
	}
	/**
	 * 
	 * 获取售前走访记录
	 * @param string $openid
	 * @param string $visit_unit 公司单位，走访单位
	 */
	public static function get_interview_record_from_weixin($openid,$visit_unit,$pageSize=5) {
		$data = array();
		$sql = "select * from weixin_visit_log where visit_unit=:visit_unit and delete_flag=0 and sales_id like '%".$openid."%' order by visit_log_id desc limit ".$pageSize;
		try {
    		$binds = array(
    			':visit_unit' => $visit_unit
    		);
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds);
    		$data = $st->fetchAll();
    	}catch(Exception $e){
            framework_static_function::write_log(__METHOD__.":".$e->getMessage(),'weixin_sql');
        }
        return $data;
	}
	/**
	 * 
	 * 根据公司名称获取售后答复内容
	 * @param string $customer_name
	 * @return array $data
	 */
	public static function get_aftermarket_record($customer_name) {
		$data = array();
		$sql = "SELECT qs.customer_info_id,ci.customer_name,repeat_content 
				FROM 
					weixin_question_solution qs,weixin_customer_info ci 
				WHERE 
					qs.customer_info_id = ci.customer_info_id AND ci.customer_name=:customer_name AND ci.delete_flag=0 
				order by 
					question_solution_id desc limit 1";
		try {
    		$binds = array(
    			':customer_name' => $customer_name
    		);
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds);
    		$data = $st->fetch();
    	}catch(Exception $e){
            framework_static_function::write_log(__METHOD__.":".$e->getMessage(),'weixin_sql');
        }
        return $data;
	}	
}